/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ItemDb.h"
#include "MudItem.h"
#include "myassert.h"
#include "Logger.h"

using namespace Chronos;

ItemDb::ItemDb() {}

ItemDb::~ItemDb() {
    for (ItemsConstIter iter = items.begin(); iter != items.end(); ++iter) {
        delete iter->second;
    }
    items.clear();
}


void ItemDb::addItem(int id, ItemFactory* factory) {
    ItemsConstIter iter = items.find(id);
    if (iter != items.end()) ASSERT_FAIL();
    items[id] = factory;
}


MudItem* ItemDb::create(int id) const {
    ItemsConstIter iter = items.find(id);
    if (iter == items.end()) {
        LOG(ERROR, "ItemDb: unknown item id %d", id);
        ASSERT_FAIL();
    }
    return &iter->second->create();
}

